/*
 * StatisticalOperator.java
 *
 * Created on October 1, 2004, 2:15 PM
 */

package edu.temple.GUS.MMA.util;
import java.util.Arrays;
/**
 *
 * @author  jeffleon
 */
public class StatisticalOperator {
    
    /** Creates a new instance of StatisticalOperator */
    public StatisticalOperator() {}
    
    public static float min( float[] inputList)
    {
        Arrays.sort(inputList);
        return inputList[0];
    }
    
    public static float max( float[] inputList, int size)
    {
        Arrays.sort(inputList);
        return inputList[size-1];
    }

    public static float median( float[] inputList, int size)
    {
        Arrays.sort(inputList);
        if ( size % 2 == 0 )
            return (inputList[(size/2)-1] + inputList[size/2])/2;
        else
            return (inputList[size/2]);
    }

    public static float mean( float[] inputList, int size)
    {
        float total = 0.0f;
        float meanValue = 0.0f;
        
        for ( int i=0; i < size; i++)
            total += inputList[i];
            
        meanValue = total / size;

        return meanValue;
    }

    public static float variance( float[] inputList, int size)
    {
        float varianceValue = 0.0f;
        
        // Definition: Variance = E(sqr(X)) -  sqr(E(X));
        // Caluculate E(sqr(X)) 
        for ( int i=0; i < size; i++)
            varianceValue += inputList[i] * inputList[i];
        
        varianceValue = varianceValue/size;
        
        // substract E(sqr(X)) by sqr(E(X)) to get variance
        varianceValue = varianceValue - mean(inputList, size) * mean(inputList, size);

        return varianceValue;
    }

    public static void main(String[] args) {

        float list1[] = {1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f};
        float list2[] = {1.0f, 2.0f, 3.0f, 4.0f, 5.0f};
        
        System.out.println("Min = " + Float.toString(min(list1)));
        System.out.println("Max = "+ Float.toString(max(list1, 6)));
        System.out.println("Median = " + Float.toString(median(list1, 6)));
        System.out.println("Mean = " + Float.toString(mean(list1, 6)));
        System.out.println("Variance = " + Float.toString(variance(list1, 6)));

        System.out.println("Min = " + Float.toString(min(list2)));
        System.out.println("Max = "+ Float.toString(max(list2, 5)));
        System.out.println("Median = " + Float.toString(median(list2, 5)));
        System.out.println("Mean = " + Float.toString(mean(list2, 5)));
        System.out.println("Variance = " + Float.toString(variance(list2, 5)));
    }
}
